/**
 * [1009] 十进制整数的反码
 *
 * @format
 * @lc app=leetcode.cn id=1009 lang=javascript
 */

// @lc code=start
/**
 * @param {number} n
 * @return {number}
 */
var bitwiseComplement = function (n) {
    if (n == 0) return 1
    const binary = []
    // 10 -> 0b
    while (n > 0) {
        binary.push(n % 2)
        n = Math.floor(n / 2)
    }
    // 取反
    for (let i = 0; i < binary.length; i++) {
        binary[i] ? (binary[i] = 0) : (binary[i] = 1)
    }
    // 0b -> 10
    for (let i = 0; i < binary.length; i++) {
        binary[i] ? (n += 2 ** i) : (n += 0)
    }
    return n
}
// @lc code=end
